package com.apestech.framework.file;

import java.io.*;
import java.util.ArrayList;

import com.apestech.framework.file.callback.ReadBufferFileCallBackHandler;
import com.apestech.framework.util.FileUtil;
import com.apestech.framework.util.StringUtil;

/**
 * 读文件行操作，saveList模式下才支持向前、后移动，或者获取某个指定索引的数据；默认模式只支持向后移动，获取当前数据
 * @author hupo
 *
 */
public class BufferFileRead extends StringUtil{
	
	private String fileName;
	private File file;
	private int index=-1;
	private BufferedReader bufRead=null;
	private String text=null;
	private boolean closed=false;
	private ArrayList<String> list;
	private boolean saveList=true;
	private boolean onlytext=true;
	
	public BufferFileRead(String fileName) throws Exception {
		this.fileName=fileName;
		this.file=new File(this.fileName);
		this.index=-1;
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), FileUtil.codeString(fileName));  //"UTF-8"
		this.bufRead = new BufferedReader(isr);
		this.list=new ArrayList<String>();
	}
	
	public String getFileName(){
		return this.fileName;
	}

	public void setOnlytext(boolean onlytext) {
		this.onlytext = onlytext;
	}

	public boolean closed(){
		return this.closed;
	}
	
	public int getFileText(ReadBufferFileCallBackHandler readBufferFileCallBackHandler, int pos) throws Exception{
		this.index=-1;
		this.text=bufRead.readLine();
		while(this.text!=null){
			if(onlytext){
				if(this.leftStr(this.text, 1).equals(",")){
					text=this.rightStr(this.text, this.text.length()-1);
				}
			}
			readBufferFileCallBackHandler.read(this.text, this.index+pos);
			this.text=bufRead.readLine();
			this.index++;
		}
		return this.index--;
	}
	
	public boolean next() throws IOException{
		if(!this.saveList){
			return fileNext();
		} else {
			if(this.index+1>=this.list.size()){
				return fileNext();
			} else {
				this.index++;
				this.text=this.list.get(this.index);
				return true;
			}
		}
	}
	
	private boolean fileNext() throws IOException{
		this.index++;
		this.text=bufRead.readLine();
		if(this.saveList){
			this.list.add(text);
		}
		return this.text!=null;
	}
	
	public boolean previous(){
		if(this.saveList){
			if(this.index>0){
				this.index--;
				this.text=this.list.get(this.index);
				return true;
			}
		}
		return false;
	}
	
	public void first() throws IOException{
		if(this.saveList){
			if(this.index==-1){
				next();
			} else {
				this.index=0;
				this.text=this.list.get(this.index);
			}
		}
	}
	
	public void last() throws IOException{
		while(next()){
		} 
	}
	
	public int getIndex(){
		return this.index;
	}
	
	public String getText(){
		return this.text;
	}
		
	public void close() throws IOException{
		this.bufRead.close();
		this.bufRead=null;
		this.file=null;
		this.closed=true;
	}
	
	public String getText(int index) throws IOException{
		if(this.saveList){
			if(index>this.index){
				while(next()){
					if(this.index==index){
						break;
					}
				}
			}
			return this.list.get(index);
		} else {
			if(this.index<index){
				while(next()){
					if(this.index==index){
						break;
					}
				}
				return this.text;
			}
			return "";
		}
	}
	
	

}
